/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uk.ac.shef.speech_r25;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 *
 * @author samf
 */
public class SpeechApp {

    ControlGui controlGui;
    SpeechState state = SpeechState.NOTHINGNESS;
    Listen listen;
    Robot robot;
//    private HashSet<String> foods;
    LinkedList<String> queue;
    boolean speaking = false;
    Map<String, String> uttToPhrase;
    boolean greeted;
    boolean recognitionActive;
    
    public static void main(String args[]) {
        SpeechApp app = new SpeechApp();
        app.start();
    }

    void addGuiRequest(String text) {
        if (text.equals("Activate")) {
            recognitionActive = true;
        }
        else if (text.equals("Deactivate")) {
    
            recognitionActive = false;
        }
    }

    void start() {
        Utils utils = new Utils();
        HashMap<String, String> configs = utils.readConfig();
        
        String os = configs.get("os");
        queue = new LinkedList<String>();
        robot = new Robot();
        controlGui = new ControlGui(this);
        controlGui.start();
        //      foods = Utils.readSet("resources/foods.txt");
        listen = new Listen(this, os);
        listen.start();
        uttToPhrase = Utils.readMap("resources/phrases.txt");
        greeted = false;
        recognitionActive = false;
    }

    public void addPhrase(String utterance) {
        String phrase = uttToPhrase.get(utterance);
        queue.add(phrase);
    }

    public void update() {
        robot.flushQueue();

        if (state == SpeechState.NOTHINGNESS) {
            if (recognitionActive) {
                state = SpeechState.ACTIVATED;
            }
            if (!greeted) {
                robot.addSpeech("Initialising. Waiting for activation.");
                greeted = true;
            }
            if (queue.isEmpty()) {
                return;
            }
            String phrase = queue.poll();
            if (phrase.equals("hello")) {
                state = SpeechState.ACTIVATED;
                recognitionActive = true;
            }
            
        }

        if (state == SpeechState.ACTIVATED) {
//            robot.addSpeech("Speech recognition activated. I'm listening. What do you want to do? Choose physical or command.");
            if (!recognitionActive) {
                state = SpeechState.NOTHINGNESS;
            }
            robot.addSpeech("Hello there. I'm listening and ready.");

            state = SpeechState.WHAT_TO_DO;
        }


        if (state == SpeechState.WHAT_TO_DO) {
            if (!recognitionActive) {
                state = SpeechState.NOTHINGNESS;
            }
            if (queue.isEmpty()) {
                return;
            }
            String phrase = queue.poll();

            if (phrase.equals("happy")) {
                robot.addAnimation("happy");
            } else if (phrase.equals("sad")) {
                robot.addAnimation("sad");
            } else if (phrase.equals("wave")) {
                robot.addAnimation("wave");
            } else if (phrase.equals("hello")) {
                robot.addSpeech("Hello there.");
            } else if (phrase.equals("left_arm_up")) {
                robot.addAnimation("left_arm");
            } else if (phrase.equals("right_arm_up")) {
                robot.addAnimation("right_arm");
            } else if (phrase.equals("what_is_your_name")) {
                robot.addSpeech("My name is Zeno. What is your name?");
            } else if (phrase.equals("what_are_you")) {
                robot.addSpeech("I am a humanoid robot. I'm listening.");
            }
            else if (phrase.equals("my_name")) {
                robot.addSpeech("Pleased to meet you Samuel. I'm listening.");
            } else if (phrase.equals("healthy_living_speech")) {
                robot.addSpeech("Healthy living includes exercise and healthy eating. I will be helping children to learn more about healthy living in schools next year.");
            }
            else if (phrase.equals("goodbye")) {
                robot.addSpeech("Goodbye. Switching off responses.");
                recognitionActive = false;
                state = SpeechState.NOTHINGNESS;
            } 
            

        /*if (state == SpeechState.WHAT_TO_DO) {
            if (queue.isEmpty()) {
                return;
            }
            if (phrase.equals("physical")) {
             state = SpeechState.PHYSICAL;
             } else if (phrase.equals("command")) {
             state = SpeechState.COMMANDS;
             }
             else {
             robot.addSpeech("I'm sorry I didn't get that. You have to choose either physical or command.");
             }
             }
        }
            
                    if (state == SpeechState.PHYSICAL) {
             robot.addSpeech("OK going into physical mode. I will give you feedback on your speed and energy consumption. Say end physical to end.");
             state = SpeechState.PHYSICAL_LISTENING;
             }
        
             if (state == SpeechState.PHYSICAL_LISTENING) {
             if (queue.isEmpty()) {
             return;
             }
             String phrase = queue.poll();
           
             */
        }

    }


}
